import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,ViewController} from 'ionic-angular';
import {SportModel} from "../../model/SportModel";
import {StorageService} from "../../providers/StorageService";

@IonicPage()
@Component({
  selector: 'page-pub-more-info',
  templateUrl: 'pub-more-info.html',
})
export class PubMoreInfoPage {
  dataType: string;
  title: string;
  data:any;
  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public viewCtrl : ViewController,
              public  sportModel:SportModel,
              public storageService:StorageService) {
    this.dataType = navParams.get('dataType');
    this.data='';//初始化data,为了让写入缓存的对象存在键
    // alert(this.storageService.read("pubSportDes"))
    this.readCaech(this.dataType);//读取缓存数据
    switch (this.dataType) {
      case "phone":
        this.title = "联系方式";
        break;
      case "sportAllowJoinPersonNum":
        this.title = "参加人数";
        break;
      case "pubSportDes":
        this.title = "活动描述";
        break;
    }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PubMoreInfoPage');
  }
  dismiss(){
    // var info ={dataType:this.dataType,data:this.data};
    if(this.dataType=="phone"){
      var phone = {phone:this.data};
      this.storageService.write('phone', phone);//写入缓存
    }
    if(this.dataType=="sportAllowJoinPersonNum"){
      var sportAllowJoinPersonNum = {sportAllowJoinPersonNum:this.data}
      this.storageService.write('sportAllowJoinPersonNum', sportAllowJoinPersonNum);//写入缓存
    }
    if(this.dataType=="pubSportDes"){
      var pubSportDes = {pubSportDes:this.data};
      this.storageService.write('pubSportDes', pubSportDes);//写入缓存
    }
    this.viewCtrl.dismiss();
    // this.navCtrl.push(PublishSport,{dataType:this.dataType,data:this.data,sportModel:this.sportModel,sportTypeImg:this.sportTypeImg});
  }

  readCaech(dataType){
    if(dataType=="pubSportDes"&&this.storageService.read("pubSportDes")!=null){
      this.data = this.storageService.read("pubSportDes")["pubSportDes"];
    }
    if(dataType=="sportAllowJoinPersonNum"&&this.storageService.read("sportAllowJoinPersonNum")!=null){
      this.data = this.storageService.read("sportAllowJoinPersonNum")["sportAllowJoinPersonNum"];
    }
    if(dataType=="phone"&&this.storageService.read("phone")!=null){
      this.data = this.storageService.read("phone")["phone"];
    }
  }

}
